//
//  NSDate+Category.m
//  dealTime
//
//  Created by xiaohuihui on 2016/12/5.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "NSDate+Category.h"
#import "HHDateFormatter.h"
#import "HHCalender.h"

@implementation NSDate (Category)

+ (NSString *)timeStringToInterval: (NSTimeInterval)timeInterval {

    // iOS 生成的时间戳是10位
    NSTimeInterval interval = timeInterval / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    return [formatter stringFromDate:date];
}

+ (NSString *)timeStringToInterval: (NSTimeInterval)timeInterval dataFormat:(NSString *)format {

    // iOS 生成的时间戳是10位
    NSTimeInterval interval = timeInterval / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    return [formatter stringFromDate:date];
}

- (NSString *)dateToRequiredString {
    if ([[HHCalender sharedCalender] isDateInToday:self]) {
        //如果是今天
        int seconds = [[NSDate date] timeIntervalSinceDate:self];
        if (seconds < 60) {
            return @"刚刚";
        } else if (seconds < 60 * 60) {
            return [NSString stringWithFormat:@"%d分钟前", seconds / 60];
        } else {
            return [NSString stringWithFormat:@"%d小时前", seconds / 3600];
        }
    } else if ([[HHCalender sharedCalender] isDateInYesterday:self]) {
        //如果是昨天 10: 10
        [HHDateFormatter sharedFormatter].dateFormat = @"昨天";
        [HHDateFormatter sharedFormatter].locale =  [NSLocale localeWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]];
        return [[HHDateFormatter sharedFormatter] stringFromDate:self];
    } else {
        //首先要取到今年是哪一年 2016
        //再取到当前的date是哪一年, 再做比较
        NSInteger thisYear = [[HHCalender sharedCalender] component:NSCalendarUnitYear fromDate: [NSDate date]];
        NSInteger dateYear = [[HHCalender sharedCalender] component:NSCalendarUnitYear fromDate: self];
        //是今年
        if (thisYear == dateYear) {
            [HHDateFormatter sharedFormatter].dateFormat = @"MM-dd";
            [HHDateFormatter sharedFormatter].locale =  [NSLocale localeWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]];
            return [[HHDateFormatter sharedFormatter] stringFromDate:self];
        }
        //往年
        else {
            [HHDateFormatter sharedFormatter].dateFormat = @"yyyy-MM-dd";
            [HHDateFormatter sharedFormatter].locale =  [NSLocale localeWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]];
            return [[HHDateFormatter sharedFormatter] stringFromDate:self];
        }
    }
}

+ (NSString *)getTimeStrWithString:(NSString *)str{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"]; //设定时间的格式
    NSDate *tempDate = [dateFormatter dateFromString:str];//将字符串转换为时间对象
    NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000];//字符串转成时间戳,精确到毫秒*1000
    return timeStr;
}

@end
